package com.inspectortime.webapp.inspector.fee;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.Repository;
import com.inspectortime.webapp.util.FlexibleNumberPropertyEditor;
import com.inspectortime.webapp.util.HttpSessionUtils;

public class FeeWizardBasicFlatFeePlanController implements Controller {

	private Repository repository;

	public ModelAndView doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("inspectorCompany.id");
		InspectorCompany company = repository.loadInspectorCompany(id);
		FeeWizardBasicFlatFeePlanHelper helper = FeeUtils.retrieveBasicFlatFeeFeeWizardHelperFromSession(request);
		if (helper == null) {
			helper = new FeeWizardBasicFlatFeePlanHelper();
		}
		BindException errors = new BindException(helper, "helper");
		FlexibleNumberPropertyEditor editor = new FlexibleNumberPropertyEditor("###,###,##0", Double.class);
		double baseFee = company.getBaseInspectionFee();
		try {
			editor.setAsText(request.getParameter("inspectorCompany.baseInspectionFee"));
			baseFee = ((Double) editor.getValue()).doubleValue();
		} catch (IllegalArgumentException e) {
			String[] errorArgs = new String[] {};
			errors.rejectValue("baseInspectionFee", "Invalid", errorArgs, "Invalid value. Enter numbers only.");
		}
		//double baseFee = Double.parseDouble(request.getParameter("inspectorCompany.baseInspectionFee"));
		if (baseFee != company.getBaseInspectionFee()) {
			company.setBaseInspectionFee(baseFee);
			company.save();
		}
				
		ModelAndView mv = null;
		
		// Failure
		if (errors.getErrorCount() > 0) {
			mv = new ModelAndView("inspector/fee/basicFlatFeePlanForm", errors.getModel());			
			mv.addObject("helper", helper);
			mv.addObject("errors", errors);
			mv.addObject("inspectorCompany", company);
			mv.addObject("mode", request.getParameter("mode"));
			return mv;
		}
		
		// Success
		if (request.getParameter("mode").equalsIgnoreCase("noWizard")) {
			mv = new ModelAndView("redirect:priceHome.html");
		} else {
			mv = new ModelAndView("redirect:feeWizardAdjusters.html");
		}		
		mv.addObject("message", "Basic flat inspection fee price was updated.");
		return mv;
	}
	
	public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
		FeeWizardBasicFlatFeePlanHelper helper = new FeeWizardBasicFlatFeePlanHelper();
		FeeUtils.storeBasicFlatFeeFeeWizardInSession(request, helper);

		InspectorCompany company = HttpSessionUtils.retrieveLoggedInCompany(request, repository);
		
		ModelAndView mv = new ModelAndView("inspector/fee/basicFlatFeePlanForm");
		mv.addObject("inspectorCompany", company);
		mv.addObject("mode", request.getParameter("mode"));
		return mv;
	}	
		
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (request.getMethod().equalsIgnoreCase("get")) {
			return doGet(request, response);
		}
			return doPost(request, response);
	}

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

}
